/*
 * Copyright (c) 2024 -present FengJinjian ( 751965763@qq.com )
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.vital.energy.longevity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.vital.energy.longevity.domain.dto.contract.ContractCreateDTO;
import edu.vital.energy.longevity.domain.dto.contract.ContractQueryDTO;
import edu.vital.energy.longevity.domain.dto.contract.ContractUpdateDTO;
import edu.vital.energy.longevity.entity.Contract;
import edu.vital.energy.longevity.mapper.ContractMapper;
import edu.vital.energy.longevity.service.IContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 合同表服务实现类
 *
 * @author Feng JinJian
 * @version 1.0.1
 * @date 2025-01-14
 */
@Slf4j
@Service
public class ContractServiceImpl implements IContractService {

    @Autowired
    private ContractMapper contractMapper;

    @Override
    public Map<String, Object> getContractPage(Integer pageNum, Integer pageSize, ContractQueryDTO query) {
        // 构建查询条件
        LambdaQueryWrapper<Contract> wrapper = new LambdaQueryWrapper<>();

        // TODO: 根据实际字段添加查询条件


        // 按更新时间倒序
        wrapper.orderByDesc(Contract::getUpdateTime);

        // 执行分页查询
        IPage<Contract> page = contractMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);

        // 包装返回结果
        Map<String, Object> result = new HashMap<>();
        result.put("records", page.getRecords());
        result.put("current", page.getCurrent());
        result.put("size", page.getSize());
        result.put("total", page.getTotal());
        result.put("pages", page.getPages());

        return result;
    }

    @Override
    public Contract getContractById(Long id) {
        Contract contract = contractMapper.selectById(id);
        if (contract == null) {
            throw new IllegalArgumentException("合同表不存在");
        }
        return contract;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createContract(ContractCreateDTO dto) {
        Contract contract = new Contract();
        BeanUtils.copyProperties(dto, contract);
        contract.setStatus((byte) 1); // 设置初始状态

        contractMapper.insert(contract);
        log.info("创建合同表成功: id={}", contract.getId());

        return contract.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateContract(Long id, ContractUpdateDTO dto) {
        Contract contract = getContractById(id);
        BeanUtils.copyProperties(dto, contract);

        int rows = contractMapper.updateById(contract);
        log.info("更新合同表成功: id={}, 影响行数={}", id, rows);
        return rows;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteContract(Long id) {
        int rows = contractMapper.deleteById(id);
        if (rows <= 0) {
            throw new IllegalArgumentException("合同表不存在");
        }
        log.info("删除合同表成功: id={}, 影响行数={}", id, rows);
        return rows;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int batchDeleteContract(List<Long> ids) {
        int rows = contractMapper.deleteBatchIds(ids);
        log.info("批量删除合同表成功: ids={}, 影响行数={}", ids, rows);
        return rows;
    }

    @Override
    public byte[] exportContract(ContractQueryDTO query) {

        throw new UnsupportedOperationException("导出功能待实现");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importContract(MultipartFile file) {

        throw new UnsupportedOperationException("导入功能待实现");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateContractStatus(Long id, Integer status) {
        Contract contract = getContractById(id);

        contractMapper.updateById(contract);
        log.info("更新合同表状态成功: id={}, status={}", id, status);
    }
}
